﻿Imports Data

Public Class IngresoPago_Proveedor

    Public Enum Acciones
        Ingresar = 1
        Modificar = 2
    End Enum


    Private Property id_pago As Integer
    Private Property accion As Integer

    Private Sub IngresoPago_Proveedor_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.CargarCBX()

        If accion = Acciones.Modificar Then
            Me.Modificar_Interfaz()
        End If
    End Sub

    Private Function Modificar_Interfaz() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim pago As Data.Pagos_Proveedores = db.Pagos_Proveedores.Where(Function(p) p.id = id_pago).Select(Function(p) p).FirstOrDefault
                Me.cbxProveedores.SelectedValue = pago.id_proveedor
                Me.txtMonto.Text = pago.monto
                Me.cbxTipoPago.SelectedValue = pago.id_modo_pago
                Me.cbxBanco.SelectedValue = pago.id_banco
                Me.txtNumeroFactura.Text = pago.numero_factura
                Me.txtDetalle.Text = pago.detalle
                Me.dtFechaPago.Value = pago.fecha_pago
                Me.CargarMes()

                Dim cheques As List(Of Cheques) = db.Cheques.Where(Function(p) p.id_factira = id_pago).Select(Function(p) p).ToList
                Me.txtCantidadCheques.Text = cheques.Count
                If cheques.Count <> 0 Then
                    Me.dtFechaPago.Value = cheques(0).fecha_cobrar
                    Me.txtMontoCheque.Text = cheques(0).monto
                End If

                Me.btnIngresar.Text = "Modificar"
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


    Private Sub CargarCBX()
        Me.CargarCBXBanco()
        Me.CargarCBXProveedor()
        Me.CargarCBXTipoPago()
        Me.CargarMes()
    End Sub

    Private Function CargarCBXProveedor() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Proveedores _
                                                        Select New AbstractObject With {.ID = x.id, _
                                                                                        .Nombre = x.nombre}).ToList

                Dim manager As BindingManagerBase = Me.cbxProveedores.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxProveedores.DisplayMember = "Nombre"
                Me.cbxProveedores.ValueMember = "ID"
                Me.cbxProveedores.DataSource = lista
                Me.cbxProveedores.Enabled = True
                manager.ResumeBinding()

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarCBXTipoPago() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Modo_Pago _
                                                        Select New AbstractObject With {.ID = x.id, _
                                                                                        .Nombre = x.nombre}).ToList

                Dim manager As BindingManagerBase = Me.cbxTipoPago.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxTipoPago.DisplayMember = "Nombre"
                Me.cbxTipoPago.ValueMember = "ID"
                Me.cbxTipoPago.DataSource = lista
                Me.cbxTipoPago.Enabled = True
                manager.ResumeBinding()

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarCBXBanco() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Banco _
                                                        Select New AbstractObject With {.ID = x.id, _
                                                                                        .Nombre = x.nombre}).ToList

                Dim manager As BindingManagerBase = Me.cbxBanco.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxBanco.DisplayMember = "Nombre"
                Me.cbxBanco.ValueMember = "ID"
                Me.cbxBanco.DataSource = lista
                Me.cbxBanco.Enabled = True
                manager.ResumeBinding()

            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarMes() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Me.txtMes.Text = db.Meses.Where(Function(p) p.id = Me.dtFechaPago.Value.Month).Select(Function(p) p.nombre).FirstOrDefault
                Me.txtMes.Enabled = False
                Return True
            End Using
        Catch ex As Exception
            Return False
        End Try
    End Function


    Private Function Ingresar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim n_pago As New Data.Pagos_Proveedores
                n_pago.id_proveedor = Me.cbxProveedores.SelectedValue
                n_pago.monto = Me.txtMonto.Text
                n_pago.id_modo_pago = Me.cbxTipoPago.SelectedValue
                n_pago.id_banco = Me.cbxBanco.SelectedValue
                n_pago.numero_factura = Me.txtNumeroFactura.Text
                n_pago.detalle = Me.txtDetalle.Text
                n_pago.fecha_pago = Me.dtFechaPago.Value
                n_pago.id_mes = Me.dtFechaPago.Value.Month
                n_pago.id_estado = 2
                db.Pagos_Proveedores.InsertOnSubmit(n_pago)
                db.SubmitChanges()
                Dim id As Integer = n_pago.id
                For i = 0 To CInt(Me.txtCantidadCheques.Text) - 1
                    Dim n_cheque As New Cheques
                    n_cheque.fecha_cobrar = Me.dtFechaPago.Value.AddMonths(i)
                    n_cheque.monto = Me.txtMontoCheque.Text
                    n_cheque.id_factira = id
                    db.Cheques.InsertOnSubmit(n_cheque)
                    db.SubmitChanges()
                Next
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function



    Public Sub New(ByVal accion As Integer, ByVal id_pago As Integer)

        ' Llamada necesaria para el diseñador.
        InitializeComponent()
        Me.id_pago = id_pago
        Me.accion = accion
    End Sub

    Private Sub dtFechaPago_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dtFechaPago.ValueChanged
        Try
            Me.CargarMes()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresar.Click
        Try
            If Me.accion = Acciones.Ingresar Then
                Me.Ingresar()
                Me.Close()
            Else
                Me.Modificar()
                Me.Close()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function Modificar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim pago As Data.Pagos_Proveedores = db.Pagos_Proveedores.Where(Function(p) p.id = Me.id_pago).Select(Function(p) p).FirstOrDefault
                pago.id_proveedor = Me.cbxProveedores.SelectedValue
                pago.monto = Me.txtMonto.Text
                pago.id_modo_pago = Me.cbxTipoPago.SelectedValue
                pago.numero_factura = Me.txtNumeroFactura.Text
                pago.detalle = Me.txtDetalle.Text
                pago.fecha_pago = Me.dtFechaPago.Value

                'lista de cheques en la base de datos
                Dim l_viejos_cheques As List(Of Data.Cheques) = db.Cheques.Where(Function(p) p.id_factira = Me.id_pago).Select(Function(p) p).ToList

                Dim l_nuevos_cheques As New List(Of Data.Cheques)
                For i = 0 To CInt(Me.txtCantidadCheques.Text) - 1
                    Dim n_cheque As New Data.Cheques
                    n_cheque.monto = Me.txtMontoCheque.Text
                    n_cheque.id_factira = Me.id_pago
                    n_cheque.fecha_cobrar = Me.dtFechaCobrarCheque.Value.AddMonths(i)
                    n_cheque.id_estado = 2

                    l_nuevos_cheques.Add(n_cheque)
                Next

                'ingresados los nuevos cheques
                db.Cheques.InsertAllOnSubmit(l_nuevos_cheques)

                'limpiando los datos antiguos
                For Each item In l_viejos_cheques
                    db.Cheques.DeleteOnSubmit(item)
                Next

                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class